﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;


namespace SIOsystem
{
    public partial class ReportForm : Form
    {
        public string uname = "";
        public long id = -1;
        public ReportForm()
        {
            InitializeComponent();
        }

        private void InQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getInOrderList(InSD.Value, InED.Value, Inkeyword.Text);
            if (dt!=null)
            {
                OrderList.DataSource = dt;
            }
        }

        private void ReportForm_Load(object sender, EventArgs e)
        {
            InSD.Value = InSD.Value.AddMonths(-1);
            OutSD.Value = OutSD.Value.AddMonths(-1);
            DeptSD.Value = DeptSD.Value.AddMonths(-1);
            ProductSD.Value = ProductSD.Value.AddMonths(-1);
            DictionaryClass.fillDepartment(ref OutDept);
            DictionaryClass.fillDepartment(ref DeptList);
            DictionaryClass.fillMainClass(ref MainClass);
        }

        private void OrderList_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            InProductName.Text = OrderList.Rows[e.RowIndex].Cells["PName"].Value.ToString();
            InAmt.Text = OrderList.Rows[e.RowIndex].Cells["Amt"].Value.ToString();
            OldPrice.Text = OrderList.Rows[e.RowIndex].Cells["Price"].Value.ToString();
            NewPrice.Value = decimal.Parse(OldPrice.Text);
        }

        private void ModInPrice_Click(object sender, EventArgs e)
        {
            if (OrderList.Rows.Count>0 && OrderClass.updateNewPrice(long.Parse(OrderList.SelectedRows[0].Cells["ISID"].Value.ToString())
                , double.Parse(NewPrice.Value.ToString()))==1)
            {
                MessageBox.Show("修改成功");
                DataTable dt = ReportClass.getInOrderList(InSD.Value.Date, InED.Value.AddDays(1).Date, Inkeyword.Text);
                if (dt != null)
                {
                    OrderList.DataSource = dt;
                }
            }
        }

        private void OutQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getOutOrderList(OutSD.Value.Date, OutED.Value.AddDays(1).Date, OutKeyword.Text, OutName.Text, long.Parse(OutDept.SelectedValue.ToString()));
            if (dt != null)
            {
                OutOrderList.DataSource = dt;
            }
        }

        private void ProductQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getInOutDetail(ProductSD.Value.Date, ProductED.Value.AddDays(1).Date, long.Parse(MainClass.SelectedValue.ToString()));
            if (dt != null)
            {
                InOutGridView.DataSource = dt;
            }
        }

        private void SumQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getDeptDetail(DeptSD.Value.Date, DeptED.Value.AddDays(1).Date, long.Parse(DeptList.SelectedValue.ToString()));
            if (dt != null)
            {
                DeptGridView.DataSource = dt;
            }
        }

        private void DeptSumExport_Click(object sender, EventArgs e)
        {
            sfd.FileName = "办公用品进出货清单.xls";
            if (sfd.ShowDialog() != DialogResult.OK) { return; }
            DataTable dt = ReportClass.getDeptDetail(DeptSD.Value.Date, DeptED.Value.AddDays(1).Date, long.Parse(DeptList.SelectedValue.ToString()));
            if (dt == null) { return; }
            if (dt.Rows.Count==0) { return; }
            dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
            dt.Columns[0].ColumnName = "编号";
            dt.Columns[1].ColumnName = "商品名称";
            dt.Columns[2].ColumnName = "规格型号";
            dt.Columns[3].ColumnName = "单位";
            dt.Columns[4].ColumnName = "数量";
            dt.Columns[5].ColumnName = "价格";
            dt.Columns[6].ColumnName = "日期";
            string[] strHeaderText = { "办公用品进出货清单", "（" + DeptSD.Value.Date.ToString("yy.MM.dd") + "-" + DeptED.Value.Date.ToString("yy.MM.dd") + "）", "" };
            if (ExcelHelper.Export(dt, strHeaderText, sfd.FileName))
            {
                MessageBox.Show("导出成功");
            } 
            else
            {
                MessageBox.Show("导出失败，请先关闭已打开文件。");
            }
        }

        private void ProductExport_Click(object sender, EventArgs e)
        {
            sfd.FileName = "办公用品进出货明细账.xls";
            if (sfd.ShowDialog() != DialogResult.OK) { return; }
            DataTable dt = ReportClass.getInOutDetail(ProductSD.Value.Date, ProductED.Value.AddDays(1).Date, long.Parse(MainClass.SelectedValue.ToString()));
            if (dt == null) { return; }
            if (dt.Rows.Count == 0) { return; }
            dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
            dt.Columns[0].ColumnName = "编号";
            dt.Columns[1].ColumnName = "商品名称";
            dt.Columns[2].ColumnName = "规格型号";
            dt.Columns[3].ColumnName = "单位";
            dt.Columns[4].ColumnName = "单价";
            dt.Columns[5].ColumnName = "上月盘存数";
            dt.Columns[6].ColumnName = "本月进货数";
            dt.Columns[7].ColumnName = "本月出货数";
            dt.Columns[8].ColumnName = "本月库存数";
            dt.Columns[9].ColumnName = "金额小计";
            string[] strHeaderText = { "办公用品进出货明细账", "（" + ProductSD.Value.Date.ToString("yy.MM.dd") + "-" + ProductED.Value.Date.ToString("yy.MM.dd") + "）", "" };
            if (ExcelHelper.Export(dt, strHeaderText, sfd.FileName))
            {
                MessageBox.Show("导出成功");
            }
            else
            {
                MessageBox.Show("导出失败，请先关闭已打开文件。");
            }
        }

        private void DeptSumPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getDeptDetail(DeptSD.Value.Date, DeptED.Value.AddDays(1).Date, long.Parse(DeptList.SelectedValue.ToString()));
            if (dt == null) { return; }
            if (dt.Rows.Count == 0) { return; }
            dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
            dt.Columns[0].ColumnName = "编号";
            dt.Columns[1].ColumnName = "商品名称";
            dt.Columns[2].ColumnName = "规格型号";
            dt.Columns[3].ColumnName = "单位";
            dt.Columns[4].ColumnName = "数量";
            dt.Columns[5].ColumnName = "价格";
            dt.Columns[6].ColumnName = "日期";
            PrintHelper ph = new PrintHelper(dt);
            ph.Print();
        }

        private void ProductPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getInOutDetail(ProductSD.Value.Date, ProductED.Value.AddDays(1).Date, long.Parse(MainClass.SelectedValue.ToString()));
            if (dt == null) { return; }
            if (dt.Rows.Count == 0) { return; }
            dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
            dt.Columns[0].ColumnName = "编号";
            dt.Columns[1].ColumnName = "商品名称";
            dt.Columns[2].ColumnName = "规格型号";
            dt.Columns[3].ColumnName = "单位";
            dt.Columns[4].ColumnName = "单价";
            dt.Columns[5].ColumnName = "上月盘存数";
            dt.Columns[6].ColumnName = "本月进货数";
            dt.Columns[7].ColumnName = "本月出货数";
            dt.Columns[8].ColumnName = "本月库存数";
            dt.Columns[9].ColumnName = "金额小计";
            PrintHelper ph = new PrintHelper(dt);
            ph.Print();
        }

    }
}
